<?php

define('THEMENAME', '');

/*
* @widget building
*/
add_action('widgets_init', 'qva_widgets_building');

// unregister_widget('');


function  qva_widgets_building()
{
    $aListWidgets = array('qva_simple_banner', 'qva_lastest_twitter', 'qva_flickr_feed', 'qva_search', 'qva_text', 'qva_categories', 'qva_tagclound', 'qva_recent_comments', 'qva_recent_posts', 'qva_calendar');

    foreach ($aListWidgets as $widget)
    {
        register_widget( $widget );
    }

    $aRemoveWidgets = array("WP_Widget_Search", "WP_Widget_Text", "WP_Widget_Categories", "WP_Widget_Tag_Cloud", "WP_Widget_Recent_Comments", "WP_Widget_Recent_Posts", "WP_Widget_Calendar");

    foreach ($aRemoveWidgets as $remove)
    {
        unregister_widget($remove);
    }
}


class qva_calendar extends WP_Widget
{
    function __construct() {
        $widget_ops = array('classname' => 'qva_calendar', 'description' => __( 'A calendar of your site&#8217;s Posts.', QVA_LANG) );
        parent::__construct('qva_calendar', __(THEMENAME . 'Calendar', QVA_LANG), $widget_ops);
    }

    function widget( $args, $instance ) {
        extract($args);


        echo $before_widget;
        echo '<div class="widget_calendar">';

        echo '<div id="calendar_wrap">';
        get_calendar();
        echo '</div>';
        echo '</div>';
        echo $after_widget;
    }

    function update( $new_instance, $old_instance ) 
    {
    }

    function form( $instance ) 
    {

    }
       
}


class qva_recent_posts extends WP_Widget
{
    function __construct() {
        $widget_ops = array('classname' => 'qva_recent_posts', 'description' => __( "Your site&#8217;s most recent Posts.", QVA_LANG) );
        parent::__construct('qva_recent_posts', __(THEMENAME . 'Recent Posts', QVA_LANG), $widget_ops);
        $this->alt_option_name = 'widget_recent_entries';

        add_action( 'save_post', array($this, 'flush_widget_cache') );
        add_action( 'deleted_post', array($this, 'flush_widget_cache') );
        add_action( 'switch_theme', array($this, 'flush_widget_cache') );
    }

    function widget($args, $instance) {
        $cache = array();
        if ( ! $this->is_preview() ) {
            $cache = wp_cache_get( 'widget_recent_posts', 'widget' );
        }

        if ( ! is_array( $cache ) ) {
            $cache = array();
        }

        if ( ! isset( $args['widget_id'] ) ) {
            $args['widget_id'] = $this->id;
        }

        if ( isset( $cache[ $args['widget_id'] ] ) ) {
            echo $cache[ $args['widget_id'] ];
            return;
        }

        ob_start();
        extract($args);

        $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts', QVA_LANG );

        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );

        $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
        if ( ! $number )
            $number = 5;
        // $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;

        /**
         * Filter the arguments for the Recent Posts widget.
         *
         * @since 3.4.0
         *
         * @see WP_Query::get_posts()
         *
         * @param array $args An array of arguments used to retrieve the recent posts.
         */
        $r = new WP_Query( apply_filters( 'widget_posts_args', array(
            'posts_per_page'      => $number,
            'no_found_rows'       => true,
            'post_status'         => 'publish',
            'ignore_sticky_posts' => true
        ) ) );

        if ($r->have_posts()) :
?>
        <?php echo $before_widget; ?>
        <div class="widget_recent_entries">
        <?php if ( $title ) echo $before_title . $title . $after_title; ?>
        <ul>
        <?php while ( $r->have_posts() ) : $r->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
            </li>
        <?php endwhile; ?>
        </ul>
        </div>
        <?php echo $after_widget; ?>
<?php
        // Reset the global $the_post as this query will have stomped on it
        wp_reset_postdata();

        endif;

        if ( ! $this->is_preview() ) {
            $cache[ $args['widget_id'] ] = ob_get_flush();
            wp_cache_set( 'widget_recent_posts', $cache, 'widget' );
        } else {
            ob_end_flush();
        }
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['number'] = (int) $new_instance['number'];
        // $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
        $this->flush_widget_cache();

        $alloptions = wp_cache_get( 'alloptions', 'options' );
        if ( isset($alloptions['widget_recent_entries']) )
            delete_option('widget_recent_entries');

        return $instance;
    }

    function flush_widget_cache() {
        wp_cache_delete('widget_recent_posts', 'widget');
    }

    function form( $instance ) {
        $title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
        $number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
        $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', QVA_LANG ); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>

        <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:', QVA_LANG ); ?></label>
        <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>

<?php
    }
}


class qva_recent_comments extends WP_Widget
{

    function __construct() {
        $widget_ops = array('classname' => 'qva_recent_comments', 'description' => __( 'Your site&#8217;s most recent comments.', QVA_LANG ) );
        parent::__construct('qva_recent_comments', __(THEMENAME . 'Recent Comments', QVA_LANG), $widget_ops);
        $this->alt_option_name = 'widget_recent_comments';

        if ( is_active_widget(false, false, $this->id_base) )
            add_action( 'wp_head', array($this, 'recent_comments_style') );

        add_action( 'comment_post', array($this, 'flush_widget_cache') );
        add_action( 'edit_comment', array($this, 'flush_widget_cache') );
        add_action( 'transition_comment_status', array($this, 'flush_widget_cache') );
    }

    function recent_comments_style() {

        /**
         * Filter the Recent Comments default widget styles.
         *
         * @since 3.1.0
         *
         * @param bool   $active  Whether the widget is active. Default true.
         * @param string $id_base The widget ID.
         */
        if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
            || ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) )
            return;
        ?>
    <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
<?php
    }

    function flush_widget_cache() {
        wp_cache_delete('widget_recent_comments', 'widget');
    }

    function widget( $args, $instance ) {
        global $comments, $comment;

        $cache = array();
        if ( ! $this->is_preview() ) {
            $cache = wp_cache_get('widget_recent_comments', 'widget');
        }
        if ( ! is_array( $cache ) ) {
            $cache = array();
        }

        if ( ! isset( $args['widget_id'] ) )
            $args['widget_id'] = $this->id;

        if ( isset( $cache[ $args['widget_id'] ] ) ) {
            echo $cache[ $args['widget_id'] ];
            return;
        }

        extract($args, EXTR_SKIP);
        $output = '';

        $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments', QVA_LANG );

        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );

        $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
        if ( ! $number )
            $number = 5;

        /**
         * Filter the arguments for the Recent Comments widget.
         *
         * @since 3.4.0
         *
         * @see get_comments()
         *
         * @param array $comment_args An array of arguments used to retrieve the recent comments.
         */
        $comments = get_comments( apply_filters( 'widget_comments_args', array(
            'number'      => $number,
            'status'      => 'approve',
            'post_status' => 'publish'
        ) ) );

        $output .= $before_widget;
        $output .= '<div class="widget_recent_comments">';
        if ( $title )
            $output .= $before_title . $title . $after_title;

        $output .= '<ul id="recentcomments">';
        if ( $comments ) {
            // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
            $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
            _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );

            foreach ( (array) $comments as $comment) {
                $output .=  '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', QVA_LANG), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
            }
        }
        $output .= '</ul>';
        $output .= '</div>';
        $output .= $after_widget;

        echo $output;

        if ( ! $this->is_preview() ) {
            $cache[ $args['widget_id'] ] = $output;
            wp_cache_set( 'widget_recent_comments', $cache, 'widget' );
        }
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['number'] = absint( $new_instance['number'] );
        $this->flush_widget_cache();

        $alloptions = wp_cache_get( 'alloptions', 'options' );
        if ( isset($alloptions['widget_recent_comments']) )
            delete_option('widget_recent_comments');

        return $instance;
    }

    function form( $instance ) {
        $title  = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
        $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', QVA_LANG ); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>

        <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of comments to show:', QVA_LANG ); ?></label>
        <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<?php
    }
}


class qva_tagclound extends WP_Widget
{
    function __construct() {
        $widget_ops = array( 'description' => __( "A cloud of your most used tags.", QVA_LANG) );
        parent::__construct('qva_tagclound', __(THEMENAME . 'Tag Cloud', QVA_LANG), $widget_ops);
    }

    function widget( $args, $instance ) {
        extract($args);
        $current_taxonomy = $this->_get_current_taxonomy($instance);
        if ( !empty($instance['title']) ) {
            $title = $instance['title'];
        } else {
            if ( 'post_tag' == $current_taxonomy ) {
                $title = __('Tags', QVA_LANG);
            } else {
                $tax = get_taxonomy($current_taxonomy);
                $title = $tax->labels->name;
            }
        }

        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );

        echo $before_widget;
        echo '<div class="widget_tag_cloud">';
        if ( $title )
            echo $before_title . $title . $after_title;

        echo '<div class="tagcloud">';

        /**
         * Filter the taxonomy used in the Tag Cloud widget.
         *
         * @since 2.8.0
         * @since 3.0.0 Added taxonomy drop-down.
         *
         * @see wp_tag_cloud()
         *
         * @param array $current_taxonomy The taxonomy to use in the tag cloud. Default 'tags'.
         */
        wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
            'taxonomy' => $current_taxonomy
        ) ) );

        echo "</div>\n";
        echo '</div>';
        echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance['title'] = strip_tags(stripslashes($new_instance['title']));
        $instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
        return $instance;
    }

    function form( $instance ) {
        $current_taxonomy = $this->_get_current_taxonomy($instance);
?>
    <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', QVA_LANG) ?></label>
    <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" /></p>
    <p><label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy:', QVA_LANG) ?></label>
    <select class="widefat" id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>">
    <?php foreach ( get_taxonomies() as $taxonomy ) :
                $tax = get_taxonomy($taxonomy);
                if ( !$tax->show_tagcloud || empty($tax->labels->name) )
                    continue;
    ?>
        <option value="<?php echo esc_attr($taxonomy) ?>" <?php selected($taxonomy, $current_taxonomy) ?>><?php echo $tax->labels->name; ?></option>
    <?php endforeach; ?>
    </select></p><?php
    }

    function _get_current_taxonomy($instance) {
        if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
            return $instance['taxonomy'];

        return 'post_tag';
    }
}


class qva_categories extends WP_Widget
{
        function __construct() {
        $widget_ops = array( 'classname' => 'qva_categories', 'description' => __( "A list or dropdown of categories.", QVA_LANG ) );
        parent::__construct('qva_categories', __(THEMENAME . 'Categories', QVA_LANG), $widget_ops);
    }

    function widget( $args, $instance ) {
        extract( $args );

        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories', QVA_LANG ) : $instance['title'], $instance, $this->id_base );

        $c = ! empty( $instance['count'] ) ? '1' : '0';


        echo $before_widget;
        if ( $title )
            echo $before_title . $title . $after_title;

        $cat_args = array('orderby' => 'name', 'show_count' => $c);


?>
        <ul>
<?php
        $cat_args['title_li'] = '';

        /**
         * Filter the arguments for the Categories widget.
         *
         * @since 2.8.0
         *
         * @param array $cat_args An array of Categories widget options.
         */
        wp_list_categories( apply_filters( 'widget_categories_args', $cat_args ) );
?>
        </ul>
<?php
      

        echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['count'] = !empty($new_instance['count']) ? 1 : 0;


        return $instance;
    }

    function form( $instance ) {
        //Defaults
        $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
        $title = esc_attr( $instance['title'] );
        $count = isset($instance['count']) ? (bool) $instance['count'] :false;
        // $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
        // $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', QVA_LANG ); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>



        <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
        <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts', QVA_LANG ); ?></label><br />

<?php
    }
}

class qva_text extends WP_Widget
{
    public function __construct() 
    {
        $widget_ops = array('classname' => 'qva_text', 'description' => __('Arbitrary text or HTML.', QVA_LANG));
        $control_ops = array('width' => 400, 'height' => 350);
        parent::__construct('qva_text', __(THEMENAME .  'Widget Text', QVA_LANG), $widget_ops, $control_ops);
    }

    public function widget( $args, $instance ) 
    {
        extract($args);

        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        /**
         * Filter the content of the Text widget.
         *
         * @since 2.3.0
         *
         * @param string    $widget_text The widget content.
         * @param WP_Widget $instance    WP_Widget instance.
         */
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        echo $before_widget;
            echo '<div class="widget_text">';
        if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
            <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
        <?php
            echo '</div>';
        echo $after_widget;
    }

    public function update( $new_instance, $old_instance ) 
    {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        if ( current_user_can('unfiltered_html') )
            $instance['text'] =  $new_instance['text'];
        else
            $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
        $instance['filter'] = isset($new_instance['filter']);
        return $instance;
    }

    public function form( $instance ) 
    {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
        $title = strip_tags($instance['title']);
        $text = esc_textarea($instance['text']);
?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', QVA_LANG); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title, QVA_LANG); ?>" /></p>

        <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>

        <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs', QVA_LANG); ?></label></p>
<?php
    }
}

class qva_search extends WP_Widget
{
    public function __construct()
    {
        $args = array('classname'=>'qva_search', 'description'=>'A search form for your site.');
        parent::__construct('qva_search', THEMENAME . 'Search Form');
    }

    public function form($instance)
    {
        $instance = wp_parse_args($instance, array("placeholder"=>"Search..."));
        ?>
        <p>
            <label for="<?php echo $this->get_field_id("placeholder") ?>">Place Holder</label>
            <input id="<?php echo $this->get_field_id("placeholder") ?>" class="widefat" type="text" value="<?php echo $instance['placeholder'] ?>">
        </p>
        <?php 
    }

    public function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        $new_instance = wp_parse_args((array) $new_instance, array( 'placeholder' => 'Search...'));
        $instance['placeholder'] = strip_tags($new_instance['placeholder']);
        return $instance;
    }

    public function widget($atts, $instance)
    {
        extract($atts);

        /** This filter is documented in wp-includes/default-widgets.php */
        $placeholder = apply_filters( 'widget_title', empty( $instance['placeholder'] ) ? '' : $instance['placeholder'], $instance, $this->id_base );

        echo $before_widget;
        ?>
        <div class="widget_search">
            <form role="search" method="get" id="searchform" class="searchform" action="<?php echo  site_url(); ?>" >
            <input type="text" value="<?php echo get_search_query() ?>" name="s" id="s" class="search" placeholder="<?php echo $instance['placeholder']; ?>" />
            <input class="s-search" type="submit" id="searchsubmit" value="" />
            </form>
        </div>
    <?php 
        echo $after_widget;
    }

}

class qva_simple_banner extends WP_Widget
{
    public function __construct()
    {
        $args = array('classname'=>'qva_simple_banner', 'description'=>'');
        parent::__construct("qva_simple_banner", THEMENAME . 'Simple Banner', $args);
    }

    public function form($instance)
    {
        $instance = wp_parse_args($instance, array('title' => 'Simple Banner', 'link_to'=>'', 'id_image' => '', ));
        ?>
        <p>
            <label for="<?php echo $this->get_field_id("title") ?>"><?php _e('Title', QVA_LANG) ?></label>
            <input type="text" name="<?php echo $this->get_field_name("title") ?>"
                   id="<?php echo $this->get_field_id("title") ?>" class="widefat"
                   value="<?php echo ltrim($instance['title']) ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id("link_to") ?>"><?php _e('Link', QVA_LANG) ?></label>
            <input type="text" name="<?php echo $this->get_field_name("link_to") ?>"
                   id="<?php echo $this->get_field_id("link_to") ?>" class="widefat"
                   value="<?php echo esc_url($instance['link_to']) ?>">
        </p>
        <p>
            <a class="photo md-blog" href="#">
                <?php
                if (!empty($instance['id_image'])) :
                    echo wp_get_attachment_image($instance['id_image'], 'qva-banner');
                    else :
                    echo '<img src="'.QVA_ADMIN_IMG . 'banner.jpg' . '">';
                endif;
                ?>
            </a>
            <input type="hidden" name="<?php echo $this->get_field_name("id_image") ?>"
                   id="<?php echo $this->get_field_id("id_image") ?>" class="widefat"
                   value="<?php echo $instance['id_image'] ?>">
        </p>
    <?php
    }

    public  function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        $instance['title']          = strip_tags($new_instance['title']);
        $instance['id_image']   = (int)$new_instance['id_image'];
        $instance['link_to']   = esc_url($new_instance['link_to']);
        return $instance;
    }

    public  function widget($atts, $instance)
    {
        extract($atts, EXTR_SKIP);
        $title  = isset($instance['title']) ? apply_filters('widget-title', $instance['title']) : '';
        $idImage = isset($instance['id_image']) ? $instance['id_image'] : null;
        $linkTo = isset($instance['link_to']) ? $instance['link_to'] : '#';

        echo '<div class="section-sidebar simple-banner">';
            if (!empty($title)) echo $before_title . $title . $after_title;
            echo '<div class="banner">';
                if (!empty($idImage)  && is_numeric($idImage)) echo '<a href="'.$linkTo.'">'.wp_get_attachment_image($idImage, 'qva-banner').'</a>';
                if (!empty($idImage) && preg_match("#http:\/\/#", $idImage)) echo '<img src="'.$idImage.'">';
            echo '</div>';
        echo '</div>';
    }

}

class qva_lastest_twitter extends WP_Widget
{
    public function __construct()
    {
        $args = array('classname'=>'qva_lastest_twitter', 'description'=>'');
        parent::__construct("qva_lastest_twitter", THEMENAME . 'Latest Tweets', $args);
    }

    public function form($instance)
    {
        $instance = wp_parse_args($instance, array('tweet_title' => 'Latest Twitter', 'tweet_consemmer_key'=>'', 'tweet_consemmer_secret'=>'', 'tweet_access_token'=>'', 'tweet_secret_access_token'=>'', 'tweet_username'=> '', 'tweet_number_of_tweet'=>''));

        $title = !isset($instance['tweet_title']) || empty($instance['tweet_title']) ? '' : strip_tags($instance['tweet_title']);
        $consumer_key = !isset($instance['tweet_consemmer_key']) || empty($instance['tweet_consemmer_key']) ? '' : strip_tags($instance['tweet_consemmer_key']);
        $consumer_secret =  !isset($instance['tweet_consemmer_secret']) || empty($instance['tweet_consemmer_secret']) ? '' : strip_tags($instance['tweet_consemmer_secret']);
        $access_token = !isset($instance['tweet_access_token']) || empty($instance['tweet_access_token']) ? '' : strip_tags($instance['tweet_access_token']);
        $secret_access_token = !isset($instance['tweet_secret_access_token']) || empty($instance['tweet_secret_access_token']) ? '' : strip_tags($instance['tweet_secret_access_token']);
        $username =  !isset($instance['tweet_username']) || empty($instance['tweet_username']) ? '' : strip_tags($instance['tweet_username']);
        // $delay_time = !isset($instance['tweet_delay_time']) || empty($instance['tweet_delay_time']) ? '' : strip_tags($instance['tweet_delay_time']);
        $number = !isset($instance['tweet_number_of_tweet']) || empty($instance['tweet_number_of_tweet']) ? '' : strip_tags($instance['tweet_number_of_tweet']);

        ?>
        <p>
            <label for="<?php echo $this->get_field_id('tweet_title') ?>">Title</label>
            <input class="widefat" type="text" id="<?php echo $this->get_field_id('tweet_title') ?>" name="<?php echo $this->get_field_name('tweet_title') ?>" value="<?php echo $title ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('tweet_consemmer_key') ?>">Consumer Key</label>
            <input class="widefat" type="text" id="<?php echo $this->get_field_id('tweet_consemmer_key') ?>" name="<?php echo $this->get_field_name('tweet_consemmer_key') ?>" value="<?php echo $consumer_key ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('tweet_consemmer_secret') ?>">Consumer Secret</label>
            <input class="widefat" type="text" id="<?php echo $this->get_field_id('tweet_consemmer_secret') ?>" name="<?php echo $this->get_field_name('tweet_consemmer_secret') ?>" value="<?php echo $consumer_secret ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('tweet_access_token') ?>">Access Token</label>
            <input class="widefat" type="text" id="<?php echo $this->get_field_id('tweet_access_token') ?>" name="<?php echo $this->get_field_name('tweet_access_token') ?>" value="<?php echo $access_token ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('tweet_secret_access_token') ?>">Secret Acess Token</label>
            <input class="widefat" type="text" id="<?php echo $this->get_field_id('tweet_secret_access_token') ?>" name="<?php echo $this->get_field_name('tweet_secret_access_token') ?>" value="<?php echo $secret_access_token ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('tweet_username') ?>">Username</label>
            <input class="widefat" type="text" id="<?php echo $this->get_field_id('tweet_username') ?>" name="<?php echo $this->get_field_name('tweet_username') ?>" value="<?php echo $username ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('tweet_number_of_tweet') ?>">The Number Of Tweets</label>
            <input class="widefat" type="text" id="<?php echo $this->get_field_id('tweet_number_of_tweet') ?>" name="<?php echo $this->get_field_name('tweet_number_of_tweet') ?>" value="<?php echo $number ?>">
        </p>
       <!--  <p>
            <label for="<?php echo $this->get_field_id('tweet_delay_time') ?>">Delay Time</label>
            <input class="widefat" type="text" id="<?php echo $this->get_field_id('tweet_delay_time') ?>" name="<?php echo $this->get_field_name('tweet_delay_time') ?>" value="<?php echo $delay_time ?>">
        </p> -->
    <?php
    }

    public  function update($new_instance, $old_instance)
    {
        $instance = $old_instance;

        foreach ($new_instance as $key => $value)
        {
            $instance[$key] = strip_tags(ltrim($value));
        }

        // update_option(TWITTER_DATA, $instance);

        return $instance;
    }

    public  function widget($atts, $instance)
    {
        extract($atts, EXTR_SKIP);

        $title = empty($instance['tweet_title']) ? '' :  $before_title .  apply_filters('tweet_title', $instance['tweet_title']) . $after_title;
        $consumer_key = empty($instance['tweet_consemmer_key']) ? '' : $instance['tweet_consemmer_key'];
        $consumer_secret = empty($instance['tweet_consemmer_secret']) ? '' : $instance['tweet_consemmer_secret'];
        $access_token = empty($instance['tweet_access_token']) ? '' : $instance['tweet_access_token'];
        $secret_access_token = empty($instance['tweet_secret_access_token']) ? '' : $instance['tweet_secret_access_token'];
        $username = empty($instance['tweet_username']) ? '' : $instance['tweet_username'];
        $delay_time = empty($instance['tweet_delay_time']) ? '' : $instance['tweet_delay_time'];
        $number = empty($instance['tweet_number_of_tweet']) ? 3 : $instance['tweet_number_of_tweet'];

        $tweetId = uniqid("tweets_");

        $res = '';

        $res .= $before_widget;

        $res .= $title;
     
        $res .= '<ul id="'.$tweetId.'" class="lastest-tweet"></ul>';
        $res .= $after_widget;


        $res .='<script type="text/javascript">';
        $res .='jQuery(document).ready(function($){';
        $res .='var tweetId = "' . "#" . $tweetId .'";';
        $res .='if( $(tweetId).length > 0 ) {';
        $res .='$(tweetId).tweet({';
        $res .='modpath: WILOKE.ajaxurl,';
        $res .='count:'. $number .',';
        $res .='loading_text: \'<p>loading Tweet...</p>\',';
        $res .='username: "'. $username .'",';
        $res .='template: \'<li>{text}</li>\'';
        $res .='});';
        $res .='}';
        $res .='})';
        $res .='</script>';


        echo $res;

    }

}

class qva_flickr_feed extends WP_Widget
{
    public function __construct()
    {
        $args = array('classname'=>'qva_flickr_feed', 'description'=>'');
        parent::__construct("qva_flickr_feed", THEMENAME . 'Flickr Feed ', $args);
    }

    public function form($instance)
    {
        $defaults = array( 'title' =>'Flickr', 'flickr_id'=>'', 'number_of_photo' => '6' , 'flickr_display' => 'latest' );
        $instance = wp_parse_args( (array) $instance, $defaults );
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title</label>
            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'flickr_id' ); ?>">Flickr ID</label>
            <input id="<?php echo $this->get_field_id( 'flickr_id' ); ?>" name="<?php echo $this->get_field_name( 'flickr_id' ); ?>" value="<?php echo $instance['flickr_id']; ?>" class="widefat" type="text" />
            <span class="help"> Find Your ID at( <a target="_blank" style="color:red" href="http://www.idgettr.com">idGettr</a> )</span>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'number_of_photo' ); ?>">Number of photos to show </label>
            <input id="<?php echo $this->get_field_id( 'number_of_photo' ); ?>" name="<?php echo $this->get_field_name( 'number_of_photo' ); ?>" value="<?php echo $instance['number_of_photo']; ?>" type="text" size="3" />
        </p>

        <?php
    }

    public  function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        $instance['title']              = strip_tags( $new_instance['title'] );
        $instance['number_of_photo']    = (int)$new_instance['number_of_photo'];
        $instance['flickr_id']          = strip_tags( $new_instance['flickr_id'] );
        return $instance;
    }

    public function widget( $args, $instance )
    {
        extract( $args, EXTR_SKIP );
        $title          = apply_filters('widget_title', $instance['title'] );
        $no_of_photos   = $instance['number_of_photo'];
        $flickr_id      = $instance['flickr_id'];


        $output = "";
        $output .= $before_widget;
        $output .= '<div class="widget_flickr">';
        if(!empty($title))
        $output .= $before_title.$title.$after_title;
        // $output.='<script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?count='.$instance['count'].'&amp;display='.$instance['order'].'&amp;size=s&amp;layout=x&amp;source=user;user='.$instance['flickrid'].'"></script>';
        $output .= '<div class="box-content">';
        $output .= "<ul id='maf-flickr-widget' class='thumbs rs clearfix'>";
        $output .= '</ul>';
        $output .= '</div>';
        $output .='<script type="text/javascript">';
        $output .= 'jQuery(document).ready(function($){';
        $output .= '$("#maf-flickr-widget").jflickrfeed({';
        $output .= 'limit: ' . $no_of_photos . ',';
        $output .= 'qstrings: {';
        $output .= "id: '".$flickr_id."',";
        $output .= '},';
        $output .= 'itemTemplate: \'<li class="transition"><a href="{{image_s}}" target="_blank"><img alt="{{title}}" src="{{image_s}}" /></a></li>\'';
        $output .= '})';
        $output .= '})';
        $output .= '</script>';
        $output .= '</div>';
        $output .= $after_widget;

        echo $output;
    }
}